<?php

/**
 * carte actions.
 *
 * @package    Librarie
 * @subpackage carte
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class carteActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	//browse
    $this->cartes = Doctrine_Core::getTable('Carte')
      ->createQuery('a')
      ->execute();
      
    /*  $isLogged = $this->getUser()->getAttribute('isUserLogged');
      if ($isLogged == NULL)
      	$this->getUser()->setAttribute('isUserLogged', false);   */
  }
  
  public function executeAddInCos(sfWebRequest $request)
  {
  	$this->getUser()->setComanda($request->getParameter('id_carte'));
  	//$this->setTemplate('cos','User');
  	$this->redirect('user/cos');
  }
  
  public function executeSearch(sfWebRequest $request)
  {
  	 $this->forwardUnless($query = $request->getParameter('query'), 'carte', 'index');
  	 
  	 $this->carti = Doctrine_Core::getTable('Carte')->getForLuceneQuery($query);
  	 //echo count($this->carti);
  }
  
  
   public function executeHomepage(sfWebRequest $request)
   {
  	//homepage
  	
   		$q = Doctrine_Query::create()->from('Carte c')->orderBy('c.data_adaugarii')->limit(10); 
   		$this->last = $q->execute();
   		
   	  $isLogged = $this->getUser()->getAttribute('isUserLogged');
      if ($isLogged == NULL)
      	$this->getUser()->setAttribute('isUserLogged', false);
   }

  public function executeShow(sfWebRequest $request)
  {
    $this->carte = Doctrine_Core::getTable('Carte')->find(array($request->getParameter('id_carte')));
    $this->isLogged = $this->getUser()->getAttribute('isUserLogged');
    //$this->forward404Unless($this->carte);

    $this->setTemplate('show');
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new CarteForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new CarteForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($carte = Doctrine_Core::getTable('Carte')->find(array($request->getParameter('id_carte'))), sprintf('Object carte does not exist (%s).', $request->getParameter('id_carte')));
    $this->form = new CarteForm($carte);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($carte = Doctrine_Core::getTable('Carte')->find(array($request->getParameter('id_carte'))), sprintf('Object carte does not exist (%s).', $request->getParameter('id_carte')));
    $this->form = new CarteForm($carte);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($carte = Doctrine_Core::getTable('Carte')->find(array($request->getParameter('id_carte'))), sprintf('Object carte does not exist (%s).', $request->getParameter('id_carte')));
    $carte->delete();

    $this->redirect('carte/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $carte = $form->save();

      $this->redirect('carte/edit?id_carte='.$carte->getIdCarte());
    }
  }
}
